package com.xinghou.platform.server.api.controller;

import com.google.gson.reflect.TypeToken;
import com.xinghou.platform.entity.req.ShareInfoReq;
import com.xinghou.platform.entity.resp.ShareInfoListResp;
import com.xinghou.platform.entity.resp.ShareInfoPhotoResp;
import com.xinghou.platform.server.api.bussiness.ShareInfoBusinessService;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.JsonUtil;
import com.xinghou.platform.util.SignUtil;
import com.xinghou.platform.util.StringUtils;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 分享信息更新
 * @author xuzhen
 *
 */
@Controller
public class UpdateShareInfoController extends BaseControl {

	private String reqjson;//请求json
	private String repjson;//返回json
	
	private ShareInfoListResp shareInfoListResp;
	private ShareInfoPhotoResp shareInfoPhotoResp;
	
	private boolean bool = true;

	@Resource
	private ShareInfoBusinessService shareInfoBusinessService;
	
	private String key = ConfigUtil.getProperty("HTTP_SERVICE_KEY");
	
	/**
	 * 分享信息更新
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateShareInfo", method = {RequestMethod.POST})
	public String updateShareInfo(@RequestBody ShareInfoReq shareInfoReq,
			HttpServletRequest request, HttpServletResponse response){
		Map<String,String> requestContent = new HashMap<String, String>();
		ShareInfoListResp shareInfoListResp = new ShareInfoListResp();
		boolean bool = true;
		try {
			if(StringUtils.isBlank(shareInfoReq.getUserid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getSharetype())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_TYPE_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getShareid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
			}else{
				bool = true;
			}

			if(bool){
				requestContent =  (Map<String, String>) JsonUtil.fromJson(reqjson,new TypeToken<Map<String, String>>() {}.getType());
				requestContent.remove("shopid");
				requestContent.remove("content");
				requestContent.remove("tasteid");

				int ret = 0;
					shareInfoListResp.setResult("0");
					
						ret = shareInfoBusinessService.updateShareCard(shareInfoReq);

					if(ret == 0){
						shareInfoListResp.setCkresult("0");
						shareInfoListResp.setErrcode("0");
						shareInfoListResp.setErrmsg("success");
					}else{
						throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
					}
					
					

			}
			
		} catch (Exception e) {
			shareInfoListResp.setResult("1");
			shareInfoListResp.setErrcode("130");
			shareInfoListResp.setErrmsg("server interface error");
		}
		
		repjson = JsonUtil.toJson(shareInfoListResp);
		return response(shareInfoListResp);
	}
	
	//---------------------------------------------NEW--------------------------------------
	/**
	 * 分享信息更新
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateNewShareInfo", method = {RequestMethod.POST})
	public String updateNewShareInfo(@RequestBody ShareInfoReq shareInfoReq,
			HttpServletRequest request, HttpServletResponse response){
		Map<String,String> requestContent = new HashMap<String, String>();
		ShareInfoListResp shareInfoListResp = new ShareInfoListResp();
		boolean bool = true;
		try {
			ShareInfoReq shareInfoReqsign = new ShareInfoReq();
			shareInfoReqsign.setUserid(shareInfoReq.getUserid());
			shareInfoReqsign.setShareid(shareInfoReq.getShareid());
			shareInfoReqsign.setSharetype(shareInfoReq.getSharetype());
			reqjson=JsonUtil.toJson(shareInfoReqsign);
			shareInfoListResp.setResult("0");
			if(StringUtils.isBlank(shareInfoReq.getUserid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getSharetype())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_TYPE_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getShareid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
			}else{
				bool = true;
			}

			if(bool){
				requestContent =  (Map<String, String>) JsonUtil.fromJson(reqjson,new TypeToken<Map<String, String>>() {}.getType());

				int ret = 0;
				
					shareInfoListResp.setResult("0");
					
						ret = shareInfoBusinessService.updateShareCardNew(shareInfoReq);

					if(ret == 0){
						shareInfoListResp.setCkresult("0");
						shareInfoListResp.setErrcode("0");
						shareInfoListResp.setErrmsg("success");
					}else{
						throw new ServiceException(DBExceptionEnums.DATA_IS_ERROR);
					}

			}
			
		} catch (Exception e) {
			e.printStackTrace();
			shareInfoListResp.setResult("1");
			shareInfoListResp.setErrcode("130");
			shareInfoListResp.setErrmsg("server interface error");
		}
		
		repjson = JsonUtil.toJson(shareInfoListResp);
		return response(shareInfoListResp);
	}
	
	/**
	 * 封面图片更新 1分享卡 2手艺人作品 3手艺人项目
	 * @param sign
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateFirstImage", method = {RequestMethod.POST})
	public String updateFirstImage(@RequestParam(required = true) String sign,@RequestBody ShareInfoReq shareInfoReq,
			HttpServletRequest request, HttpServletResponse response){
		Map<String,String> requestContent = new HashMap<String, String>();
		ShareInfoListResp shareInfoListResp = new ShareInfoListResp();
		boolean bool = true;
		try {
			ShareInfoReq shareInfoReqsign = new ShareInfoReq();
			shareInfoReqsign.setUserid(shareInfoReq.getUserid());
			shareInfoReqsign.setShareid(shareInfoReq.getShareid());
			shareInfoReqsign.setSharetype(shareInfoReq.getSharetype());
			reqjson=JsonUtil.toJson(shareInfoReqsign);
			shareInfoListResp.setResult("0");
			if(StringUtils.isBlank(shareInfoReq.getUserid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getSharetype())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_TYPE_IS_NULL);
			}else if(StringUtils.isBlank(shareInfoReq.getShareid())){
				bool = false;
				throw new ServiceException(DBExceptionEnums.SHARE_ID_IS_NULL);
			}else{
				bool = true;
			}

			if(bool){
				requestContent =  (Map<String, String>) JsonUtil.fromJson(reqjson,new TypeToken<Map<String, String>>() {}.getType());
				int ret = 0;
				if(sign.equals(SignUtil.createSign(new TreeMap<String, String>(requestContent), key).toString())){
					shareInfoListResp.setResult("0");
					ret = shareInfoBusinessService.updatSharedCardImage(shareInfoReq);
					if(ret > 0){
						shareInfoListResp.setCkresult("0");
						shareInfoListResp.setErrcode("0");
						shareInfoListResp.setErrmsg("success");
					}else{
						throw new ServiceException(DBExceptionEnums.DATA_IS_ERROR);
					}
				}else{
					shareInfoListResp.setResult("1");
					shareInfoListResp.setErrcode("140");
					shareInfoListResp.setErrmsg("signatures error");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			shareInfoListResp.setResult("1");
			shareInfoListResp.setErrcode("130");
			shareInfoListResp.setErrmsg("server interface error");
		}
		repjson = JsonUtil.toJson(shareInfoListResp);
		return response(shareInfoListResp);
	}
	
	public String getReqjson() {
		return reqjson;
	}

	public void setReqjson(String reqjson) {
		this.reqjson = reqjson;
	}

	public String getRepjson() {
		return repjson;
	}

	public void setRepjson(String repjson) {
		this.repjson = repjson;
	}

	public ShareInfoListResp getShareInfoListResp() {
		return shareInfoListResp;
	}

	public void setShareInfoListResp(ShareInfoListResp shareInfoListResp) {
		this.shareInfoListResp = shareInfoListResp;
	}

	public ShareInfoPhotoResp getShareInfoPhotoResp() {
		return shareInfoPhotoResp;
	}

	public void setShareInfoPhotoResp(ShareInfoPhotoResp shareInfoPhotoResp) {
		this.shareInfoPhotoResp = shareInfoPhotoResp;
	}
	
	
}
